package pyx.crazycards.server.core;

import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javaactor.library.core.Message;
import javaactor.library.core.Registry;
import javaactor.library.core.exception.ActorInvalidDefinitionException;

public class Server implements Runnable {

	private int port;
	
	public Server(int port) throws ActorInvalidDefinitionException {
		Registry.register(ReplyRemoteActor.class);
		Registry.register(RouterActor.class);
		Registry.register(AuthenticatorActor.class);
		Registry.register(GameLogicActor.class);
		this.port = port;
	}

	public static void main(String[] args)
			throws ActorInvalidDefinitionException {
		new Thread(new Server(8888)).start();
	}

	@Override
	public void run() {
		try {

			ServerSocket server = new ServerSocket(port);

			while(true) {
				Socket sock = server.accept();
			
				ObjectInputStream stream = new ObjectInputStream(
					sock.getInputStream());
				Message msg = (Message) stream.readObject();

				Registry.send("Router", msg);
			}
			
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

}
